﻿Imports Microsoft.Office.Interop.Outlook

Public Class SharedFunctions
    Public Shared Function GetInboxFolder(ByVal app As Application) As MAPIFolder
        Return app.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    End Function
    Public Shared Function GetContactsFolder(ByVal app As Application) As MAPIFolder
        Return app.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
    End Function
    Public Shared Function GetSelectedFolder(ByVal app As Application) As MAPIFolder
        Return app.GetNamespace("MAPI").Application.ActiveExplorer.CurrentFolder
    End Function

    Public Shared Function DateWithin48Workhours(ByVal dateToWorkWith As DateTime) As DateTime
        Dim newDate As DateTime

            Select Case dateToWorkWith.DayOfWeek
                Case DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday
                    newDate = DateAdd(DateInterval.Day, 2, dateToWorkWith)
                Case DayOfWeek.Thursday, DayOfWeek.Friday
                    newDate = DateAdd(DateInterval.Day, 4, dateToWorkWith)
            Case DayOfWeek.Saturday
                newDate = DateAdd(DateInterval.Day, 4, dateToWorkWith)
                newDate = set8oclock(newDate)
            Case DayOfWeek.Sunday
                newDate = DateAdd(DateInterval.Day, 3, dateToWorkWith)
                newDate = set8oclock(newDate)
        End Select
 
        Return newDate
    End Function

    Public Shared Function IsWorkDay(ByVal dateToWorkWith As Date) As Boolean
        Return dateToWorkWith.DayOfWeek >= 1 And dateToWorkWith.DayOfWeek <= 5
    End Function

    Public Shared Function set8oclock(ByVal dateToWorkWith As DateTime) As DateTime
        Return New DateTime(dateToWorkWith.Year, dateToWorkWith.Month, dateToWorkWith.Day, 8, 0, 0)
    End Function

    Public Shared Function GetDPNumberOutOfString(ByVal stringToParse As String) As String
        Dim DPnummer As String = String.Empty

        If stringToParse IsNot Nothing Then
            Dim DPindex As Integer = stringToParse.IndexOf("[DP01-")
            If DPindex <> -1 AndAlso stringToParse.Length > 18 Then
                DPnummer = stringToParse.Substring(DPindex + 1, 18)
                DPnummer = RemoveLeadingZeros(DPnummer.Substring(5, 10))
            End If
        End If
        
        Return DPnummer
    End Function

    Public Shared Function RemoveLeadingZeros(ByVal stringToParse As String) As String
        If String.IsNullOrEmpty(stringToParse) Then
            Return String.Empty
        End If

        Dim temp As String = stringToParse

        While (temp.Length > 0 AndAlso Left(temp, 1) = "0")
            temp = Right(temp, temp.Length - 1)
        End While

        Return temp
    End Function

    Public Shared Function contactFullnameByEmail(ByVal app As Application, ByVal emailadress As String) As String
        Dim contacts As Outlook.MAPIFolder = SharedFunctions.GetContactsFolder(app)
        Dim contactitems As Outlook.Items = contacts.Items
        contactitems = contactitems.Restrict("[MessageClass] = 'IPM.Contact'")

        For Each contact As ContactItem In contactitems
            If contact.Email1Address IsNot Nothing AndAlso contact.Email1Address.Contains(emailadress) Then
                Return contact.FullName
            End If
        Next

        Return Nothing
    End Function
End Class



